﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Wicresoft.JDPostBoard.Entity;
using Wicresoft.Common;

public partial class Gift_LeaderVerifyPageList : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 全部申请";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1 and IsEnable='True' and ApplierDepartment='" + CurrentUser.GetMainDepartment().DisplayName + "'";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Wicresoft.Framework.Organization.OrganizationPublicHelper.IsUserInRole(CurrentUser.ID, new Guid(System.Configuration.ConfigurationManager.AppSettings["OfficeLeaderID"].ToLower())))
        {
            Response.Write("对不起，您无权查看此页！<br /><br />");
            Response.Write("您可以查看<a href='MyApplyList.aspx'>本人申请页面</a>");
            Response.End();
        }

        this.Title = "申请审批列表";
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "return searchClick();");

        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ApplyTime";
            this.Order.OrderDirection = "DESC";

            this.ddlTime.Items.Add("申请时间");
            this.ddlTime.Items.Add("审核时间");
            this.ddlTime.Items.Add("到货时间");
            this.ddlTime.Items.Add("确认时间");
            this.ddlTime.Items.Insert(0, new ListItem("--请选择--", string.Empty));

            this.cbSubmit.Checked = true;

            SearchClause = string.Format("{0}='{1}'",ApplyLog.SQLCOL_AUDITORID,CurrentUser.ID);
            SearchClause += string.Format(" AND ({0}='{1}' or {0}='{2} 'or {0}='{3}' )", ApplyLog.SQLCOL_STATUS, Enum.ApplyStatusEnum.待审核.ToString(), Enum.ApplyStatusEnum.延后审核.ToString(),Enum.ApplyStatusEnum.延后审核已发货.ToString());
            BindData();
        }
    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;
        DTData = DataAccess.Select(typeof(ApplyLog), Pager.PageIndex, Pager.PageSize, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        Pager.RecordCount = recordCount;
        rptOrder.DataSource = DTData;
        rptOrder.DataBind();
    }

    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //switch (e.CommandName)
        //{
        //    //case "Pass":
        //    //    OrderList ol = new OrderList(int.Parse(e.CommandArgument.ToString()));
        //    //    ol.Load();
        //    //    if (ol.Status == "申购提交")
        //    //    {
        //    //        ol.Status = "审核通过";
        //    //        ol.Save();
        //    //    }
        //    //    BindData();
        //    //    break;
        //    case "Cancel":
        //        OrderList ol = new OrderList(int.Parse(e.CommandArgument.ToString()));
        //        ol.Load();
        //        if (ol.Status == "申购提交")
        //        {
        //            ol.Status = "审核不通过";
        //            ol.Save();
        //        }
        //        BindData();
        //        break;

        //}
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause = string.Format("({0} LIKE N'%{1}%' OR {2} LIKE N'%{1}%') and {3}='{4}'", ApplyLog.SQLCOL_APPLYTITLE, tbSearch.Text.Replace("'", "''").Replace("%", "[%]"), ApplyLog.SQLCOL_APPLIERDEPARTMENT, ApplyLog.SQLCOL_AUDITORID, CurrentUser.ID);
        else
            SearchClause = string.Format("{0}='{1}'",ApplyLog.SQLCOL_AUDITORID,CurrentUser.ID);
        //Pager.ResetPager();
        //SearchClause += string.Format(" AND IsEnable='True' AND ApplierDepartment='" + CurrentUser.GetMainDepartment().DisplayName + "' AND {0}={1}", ApplyLog.SQLCOL_STATUS, "'待审核'");

        BindData();

    }

    protected void AllPass_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptOrder.Items)
        {
            CheckBox cb = item.FindControl("cbSelect") as CheckBox;
            if (cb.Checked)
            {
                DataRow dr = DTData.Rows[item.ItemIndex];
                ApplyLog ol = new ApplyLog(new Guid(dr["ApplyID"].ToString()));
                ol.Load();
                if (ol.Status == "延后审核已发货")
                {
                    ol.Status = Enum.ApplyStatusEnum.已领取.ToString();
              
                }
                else 
                { 

                ol.Status = Enum.ApplyStatusEnum.审核通过.ToString();
                }
                ol.DealTime = DateTime.Now;
                ol.Save();
            }
        }

        BindData();

    }

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1 AND IsEnable='True' AND ApplierDepartment='" + CurrentUser.GetMainDepartment().DisplayName + "' ";
        int flag = 0;
        int StartFlag = 0;
        string TempSearch = null;
        if (tbTitle.Text != string.Empty)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", ApplyLog.SQLCOL_APPLYTITLE, tbTitle.Text.Replace("'", "''").Replace("%", "[%]"));

        if (this.tbStartTime.Text != "" && this.tbEndTime.Text != "")
        {
            if (Convert.ToDateTime(this.tbStartTime.Text) > Convert.ToDateTime(this.tbEndTime.Text))
            {
                Response.Write("<script>alert('开始日期不能小于结束日期');</script>");
                return;
            }
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_APPLYTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_DEALTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_ARRIVALTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_CONFIRMTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else if (this.tbStartTime.Text != "")
        {
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_APPLYTIME, tbStartTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_DEALTIME, tbStartTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_ARRIVALTIME, tbStartTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_CONFIRMTIME, tbStartTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else if (this.tbEndTime.Text != "")
        {
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_APPLYTIME, tbEndTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_DEALTIME, tbEndTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_ARRIVALTIME, tbEndTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_CONFIRMTIME, tbEndTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else
        { }

        if (cbSubmit.Checked)
        {
            //SearchClause += string.Format(" AND {0} = '{1}'", OrderList.SQLCOL_STATUS, cbSubmit.Text);
            flag = 1;
            SearchClause += " AND ";
            TempSearch = string.Format("{0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbSubmit.Text);

        }
        if (cbPass.Checked)
        {
            if (flag == 0)
            {
                //SearchClause += string.Format(" AND {0} = '{1}'", OrderList.SQLCOL_STATUS, cbPass.Text);
                SearchClause += " AND ";
                TempSearch = string.Format("{0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbPass.Text);
                flag = 1;
            }
            else if (flag == 1)
            {
                SearchClause += string.Format(" ( {2} OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbPass.Text, TempSearch.ToString());
                StartFlag = 1;
            }
        }
        if (cbNotPass.Checked)
        {
            if (flag == 0)
            {
                //SearchClause += string.Format(" AND {0} = '{1}'", OrderList.SQLCOL_STATUS, cbNotPass.Text);
                SearchClause += " AND ";
                TempSearch = string.Format("{0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbNotPass.Text);
                flag = 1;
            }
            else if (flag == 1)
            {
                if (StartFlag == 0)
                {
                    SearchClause += string.Format(" ( {2} OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbNotPass.Text, TempSearch.ToString());
                    StartFlag = 1;
                }
                else if (StartFlag == 1)
                {
                    SearchClause += string.Format(" OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbNotPass.Text);
                }
            }
        }
        if (cbArrival.Checked)
        {
            if (flag == 0)
            {
                SearchClause += " AND ";
                TempSearch = string.Format("{0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbArrival.Text);
                flag = 1;
            }
            else if (flag == 1)
            {
                if (StartFlag == 0)
                {
                    SearchClause += string.Format(" ( {2} OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbArrival.Text, TempSearch.ToString());
                    StartFlag = 1;
                }
                else if (StartFlag == 1)
                {
                    SearchClause += string.Format(" OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbArrival.Text);
                }
            }
        }
        if (cbConfirm.Checked)
        {
            if (flag == 0)
            {
                SearchClause += " AND ";
                SearchClause += string.Format("{0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbConfirm.Text);
                flag = 1;
            }
            else if (flag == 1)
            {
                if (StartFlag == 0)
                {
                    SearchClause += string.Format(" ( {2} OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbConfirm.Text, TempSearch.ToString());
                    StartFlag = 1;
                }
                else if (StartFlag == 1)
                {
                    SearchClause += string.Format(" OR {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbConfirm.Text);
                }
            }
        }
        if (StartFlag == 1)
            SearchClause += " ) ";
        if (flag == 1 && StartFlag == 0)
            SearchClause += TempSearch.ToString();
        if (flag == 0)
            SearchClause = "1=0";
        //if (ddlTime.Text = "")
        //    SearchClause += string.Format(" AND {0} = '{1}'", Constitution_Guider.SQLCOL_SEX, ddlSex.SelectedValue);
        //Pager.ResetPager();


        BindData();
    }
}
